package com.moon.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author thy
 * @description：todo
 * @date 2024/9/10 14:34
 */
@RestController
//@SaCheckLogin
public class LoginController {
    private final static List<String> list = Arrays.asList("去钓鱼", "不去钓鱼");

    @RequestMapping("goFishing")
    public String goFishing() {
        Collections.shuffle(list);
        return list.stream().findFirst().get() ;
    }

    @RequestMapping("test")
    @SaCheckPermission("sys:test")
    public String test() {
        StpUtil.checkPermission("sys:test");
        StpUtil.logout(1);
        return "test";
    }

    @RequestMapping("logout")
    public String logout() {
        StpUtil.logout(StpUtil.getLoginId());
        return "logout";
    }

    @RequestMapping("update")
    @SaCheckPermission("sys:update")
    public String update() {
        System.out.println(StpUtil.hasPermission("sys:test"));
        return "update";
    }

    @RequestMapping("login")
    public String login(@RequestParam String name, @RequestParam String password) {
        if ("tong".equals(name) && "123".equals(password)) {
            StpUtil.login(1);
        } else {
            throw new NotLoginException("没登陆", NotLoginException.DEFAULT_MESSAGE, "1");
        }
        return StpUtil.getTokenValue();
    }
}
